package com.example.filemonitor;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;

// 一加手机需要此接收器确保重启后仍能运行
public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            SharedPreferences prefs = context.getSharedPreferences("FileMonitorPrefs", Context.MODE_PRIVATE);
            String sourceUri = prefs.getString("source_uri", "");
            String targetPicUri = prefs.getString("target_pic_uri", "");
            String targetVioUri = prefs.getString("target_vio_uri", "");

            // 如果已设置过文件夹路径，则自动启动服务
            if (!sourceUri.isEmpty() && !targetPicUri.isEmpty()) {
                Intent serviceIntent = new Intent(context, FileMonitorService.class);
                serviceIntent.putExtra("source_uri", sourceUri);
                serviceIntent.putExtra("target_pic_uri", targetPicUri);
                serviceIntent.putExtra("target_vio_uri", targetVioUri);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(serviceIntent);
                } else {
                    context.startService(serviceIntent);
                }
            }
        }
    }
}
    